package net.int0r.gscore.util;

import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.log4j.Logger;

public class GowallaConnector {
	private static Logger log = Logger.getLogger(GowallaConnector.class);
	
	public static String getBaseURL() {
		return SettingGetter.getUrlTransport()+"://"+SettingGetter.getGowallaUName()+":"+
		SettingGetter.getGowallaUPassword()+"@"+SettingGetter.getBaseDomain();
	}

	public static GJSONObject getJSONObj(ArrayList<NameValuePair> urlParms, String path) {
		HttpClient httpclient = new DefaultHttpClient();
		HttpGet get = new HttpGet(getBaseURL()+path+"?"+URLEncodedUtils.format(urlParms, "UTF-8"));
		get.addHeader("X-Gowalla-API-Key", SettingGetter.getApiKey());
		get.addHeader("Accept", "application/json");
		try {
			HttpResponse resp = httpclient.execute(get);
			String response = GStringUtil.convertStreamToString(resp.getEntity().getContent());
			resp.getEntity().consumeContent(); // release the resources
			log.debug(get.getURI());
			log.debug(response);
			return new GJSONObject(response);
		}
		catch(Exception ex) {
			throw new RuntimeException(ex);
		}
	}
}
